package com.cumt.threeway.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.adapter.StrategyAdapter;
import com.cumt.threeway.constant.URLS;
import com.cumt.threeway.model.Strategy;
import com.cumt.threeway.table.GonglueEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.cumt.threeway.util.ThreeWayUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 胡晶晶 on 2016/5/24.
 */
public class MyStrategyActivity extends FragmentActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
    private TextView mTvNoInfo;
    protected ListView mListView;
    protected List<Strategy> mList;
    private RequestQueue queue;
    private List<GonglueEntity> mLocalList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_strategy_layout);
        initView();
//        getDataFromServer();
        getData();
    }

    private void getData() {
        mLocalList = ThreeWayData.getInstance().getMyGonglueInfo();
        StrategyAdapter checkSubmitInfoAdapter = new StrategyAdapter(mLocalList, MyStrategyActivity.this);
        mListView.setAdapter(checkSubmitInfoAdapter);
    }

    private void initView() {
        queue = Volley.newRequestQueue(this);
        mList = new ArrayList<Strategy>();
        mListView = (ListView) findViewById(R.id.lv);
        mListView.setOnItemClickListener(this);
        mTvNoInfo = (TextView) findViewById(R.id.tv_no_strategy_info);
        findViewById(R.id.btn_back).setOnClickListener(this);
    }

    private void getDataFromServer() {
        String url = URLS.IP + "NetServlet/SelectMyGonglue?loginname=" + ThreeWayUtil.getInstance().getLoginAccount(this);
        final Gson g = new Gson();
        JsonArrayRequest jar;
        jar = new JsonArrayRequest(Request.Method.POST, url, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        String data = response.toString();
                        if("[]".equals(data)) {
                            mTvNoInfo.setVisibility(View.VISIBLE);
                            mListView.setVisibility(View.GONE);
                        } else {
                            mList = g.fromJson(data, new TypeToken<List<Strategy>>() {}.getType());
//                            StrategyAdapter checkSubmitInfoAdapter = new StrategyAdapter(mList, MyStrategyActivity.this);
//                            mListView.setAdapter(checkSubmitInfoAdapter);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        }){
        };
        queue.add(jar);
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Strategy strategy = mList.get(position);
        GonglueEntity gonglueEntity = mLocalList.get(position);
        Intent intent = new Intent(MyStrategyActivity.this, StrategyInfoActivity.class);
        intent.putExtra("strategy", gonglueEntity);
        intent.putExtra("flag", "my");
        startActivity(intent);
    }
}
